/*
 * Copyright 2012 Benoît Combe <benoit.combe@gmail.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package fr.ipst.spot.formulaires.entites;

import fr.ipst.spot.entites.AgentCnam;
import fr.ipst.spot.entites.utils.RoleAgentCnam;
import fr.ipst.spot.exceptions.SpotExceptionForm;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Benoît Combe <benoit.combe@gmail.com>
 */
public class FormAgentCnam extends FormAbstractHtmlEntite<AgentCnam> {

    private static final long serialVersionUID = 1L;
    private String idLDAP;
    private String mail;
    private String nom;
    private String prenom;
    private RoleAgentCnam roleAgentCnam;

    /**
     *
     * @param entity
     */
    public FormAgentCnam(AgentCnam entity) {
        super(new FormAgentCnamId(entity.getId()), entity);
        this.idLDAP = entity.getIdLDAP();
        this.mail = entity.getMail();
        this.nom = entity.getNom();
        this.prenom = entity.getPrenom();
        this.roleAgentCnam = entity.getRole();
    }

    /**
     *
     * @param request
     */
    public FormAgentCnam(HttpServletRequest request) {
        super(new FormAgentCnamId(request), request);
        this.idLDAP = this.getNonNullStringFromRequest(request, "idLDAP");
        this.mail = this.getNonNullStringFromRequest(request, "mail");
        this.nom = this.getNonNullStringFromRequest(request, "nom");
        this.prenom = this.getNonNullStringFromRequest(request, "prenom");
        this.roleAgentCnam = RoleAgentCnam.getRoleAgentCnameByCodeSql(this.getIntegerFromRequest(request, "roleAgentCnam", -1));
    }

    @Override
    protected void validate(boolean creation) throws SpotExceptionForm {
        boolean erreur = false;
        if (!creation) {
            try {
                this.formId.validate();
            } catch (SpotExceptionForm e) {
                erreur = true;
                this.concatErreurMsg(e);
            }
        } else {
            if (this.roleAgentCnam == null) {
                erreur = true;
                this.concatErreurMsg(new SpotExceptionForm("Role Agent Cnam invalide"));
            }
            try {
                this.isIdLDAP(idLDAP);
            } catch (SpotExceptionForm e) {
                erreur = true;
                this.concatErreurMsg(e);
            }
        }
        try {
            this.isDefaultLengthOneLine(nom, "nom");
        } catch (SpotExceptionForm e) {
            erreur = true;
            this.concatErreurMsg(e);
        }
        try {
            this.isDefaultLengthOneLine(prenom, "prenom");
        } catch (SpotExceptionForm e) {
            erreur = true;
            this.concatErreurMsg(e);
        }
        try {
            this.isEmailAdresse(mail);
        } catch (SpotExceptionForm e) {
            erreur = true;
            this.concatErreurMsg(e);
        }

        if (erreur) {
            throw new SpotExceptionForm(msgErreurValidate);
        }
    }

    @Override
    public AgentCnam getEntityToPersist() throws SpotExceptionForm {
        return this.garnir(new AgentCnam(), true);
    }

    @Override
    protected AgentCnam garnir(AgentCnam entityToFeed, boolean creation) throws SpotExceptionForm {
        if (creation) {
            entityToFeed.setRole(roleAgentCnam);
            entityToFeed.setIdLDAP(idLDAP);
        }
        entityToFeed.setMail(mail);
        entityToFeed.setNom(nom);
        entityToFeed.setPrenom(prenom);
        return entityToFeed;
    }

    /**
     *
     * @return
     */
    public String getIdLDAP() {
        return idLDAP;
    }

    /**
     *
     * @param idLDAP
     */
    public void setIdLDAP(String idLDAP) {
        this.idLDAP = idLDAP;
    }

    /**
     *
     * @return
     */
    public String getMail() {
        return mail;
    }

    /**
     *
     * @param mail
     */
    public void setMail(String mail) {
        this.mail = mail;
    }

    /**
     *
     * @return
     */
    public String getNom() {
        return nom;
    }

    /**
     *
     * @param nom
     */
    public void setNom(String nom) {
        this.nom = nom;
    }

    /**
     *
     * @return
     */
    public String getPrenom() {
        return prenom;
    }

    /**
     *
     * @param prenom
     */
    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    /**
     *
     * @return
     */
    public RoleAgentCnam getRoleAgentCnam() {
        return roleAgentCnam;
    }

    /**
     *
     * @param roleAgentCnam
     */
    public void setRoleAgentCnam(RoleAgentCnam roleAgentCnam) {
        this.roleAgentCnam = roleAgentCnam;
    }
}
